package cn.kkcxy.blog.system.service.impl.sys;

import cn.kkcxy.blog.common.constant.ExceptionMessageConstant;
import cn.kkcxy.blog.core.entity.sys.SysUserRole;
import cn.kkcxy.blog.core.exception.common.MethodParameterException;
import cn.kkcxy.blog.system.mapper.sys.SysUserRoleMapper;
import cn.kkcxy.blog.system.service.sys.SysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author kk
 * @description 针对表【sys_user_role(用户角色)】的数据库操作Service实现
 * @createDate 2024-09-11 00:50:27
 */
@Slf4j
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    /**
     * 系统用户角色关联mapper
     */
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * @description: 插入用户角色关联信息
     * @author: kkcxy
     * @date: 2024/9/12 1:33
     * @param: [userId, id]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean saveUserRole(Long userId, Long roleId) {
        // 参数异常就抛异常
        if (userId == null || roleId == null) {
            if (log.isErrorEnabled()) {
                log.error("SysUserRoleServiceImpl.saveUserRole.error: {}", ExceptionMessageConstant.METHOD_PARAMETER_ERROR);
            }
            throw new MethodParameterException(ExceptionMessageConstant.METHOD_PARAMETER_ERROR);
        }
        // 封装数据
        SysUserRole sysUserRolePO = new SysUserRole();
        sysUserRolePO.setUserId(userId);
        sysUserRolePO.setRoleId(roleId);
        // 插入数据
        int row = sysUserRoleMapper.insert(sysUserRolePO);
        // 返回查询结果
        return row <= 0;
    }
}




